package com.jettech.oa.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jettech.oa.domain.Resource;
import com.jettech.oa.domain.Role;
import com.jettech.oa.domain.User;

@Repository("userDAO")
public class UserDAO extends GenericDAOHibernate<User,Long>{
	@Autowired
	public UserDAO(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
	}
	@SuppressWarnings("unchecked")
	public boolean validateUserLoginNameExist(String loginName) {
		String hql = "select count(*) from com.jettect.oa.domain.User where loginName = ? ";
		List<Long> list = this.getHibernateTemplate().find(hql, loginName);
		if(list!=null && list.size()>0){
			if(list.get(0)!=null && list.get(0)>=1){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	@SuppressWarnings("unchecked")
	public User getUser(String loginName) {
		String hql = "select id,loginName,loginPassword,status,role.id from com.jettect.oa.domain.User where loginName = ?";
		List<Object[]> list = this.getHibernateTemplate().find(hql, loginName);
		if(list!=null && list.size()>0){
			User user = new User();
			Object[] objs = list.get(0);
			user.setId((Long)objs[0]);
			user.setLoginName((String)objs[1]);
			user.setLoginPassword((String)objs[2]);
			user.setStatus((String)objs[3]);
			if(objs[4]!=null){
				Role role = new Role();
				role.setId((Long)objs[4]);
				user.setRole(role);
			}
			return user;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Resource> findUserOperationResourceList(String loginName) {
		String hql = "select resource_.id,resource_.name,resource_.parentResource.id,resource_.url,resource_.isMenu from Resource resource_ left join resource_.roles roles_ where roles_.id = (select user_.role.id from com.jettect.oa.domain.User user_ where user_.loginName = ?)";
		List<Object[]> list = this.getHibernateTemplate().find(hql,loginName);
		if(list!=null && list.size()>0){
			List<Resource> resources = new ArrayList<Resource>();
			for(Object[] objs:list){
				Resource resource = new Resource();
				resource.setId((Long)objs[0]);
				resource.setName((String)objs[1]);
				if(objs[2]!=null){
					Resource parentResource = new Resource();
					parentResource.setId((Long)objs[2]);
					resource.setParentResource(parentResource);
				}
				resource.setUrl((String)objs[3]);
				resource.setIsMenu((String)objs[4]);
				resources.add(resource);
			}
			return resources;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Resource> findUserOperationResourceList(Long roleId) {
		String hql = "select resource_.id,resource_.name,resource_.parentResource.id,resource_.url,resource_.isMenu from Resource resource_ left join resource_.roles roles_ where roles_.id = ?";
		List<Object[]> list = this.getHibernateTemplate().find(hql,roleId);
		if(list!=null && list.size()>0){
			List<Resource> resources = new ArrayList<Resource>();
			for(Object[] objs:list){
				Resource resource = new Resource();
				resource.setId((Long)objs[0]);
				resource.setName((String)objs[1]);
				if(objs[2]!=null){
					Resource parentResource = new Resource();
					parentResource.setId((Long)objs[2]);
					resource.setParentResource(parentResource);
				}
				resource.setUrl((String)objs[3]);
				resource.setIsMenu((String)objs[4]);
				resources.add(resource);
			}
			return resources;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<String> findUserLoginNameOfMyDepartment(Long documentId) {
		String hql = "select loginName from com.jettect.oa.domain.User user_ where user_.department.id = ( select document_.user.department.id from Document document_ where document_.id = ? )";
		return this.getHibernateTemplate().find(hql,documentId);
	}
}
